package cn.dankal.flyaudio.adapter;

import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.pojo.MessageEntity;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.business.pojo.NotificationBean;
import cn.dankal.flyaudio.R;

public class MessageCenterAdapter extends BaseRecyclerViewAdapter<NotificationBean> {
    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_message;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }
    private String messageType;

    public void setMessageType(String messageType) {
        this.messageType = messageType;
    }

    public String getMessageType() {
        return messageType;
    }

    public class ViewHolder extends BaseRecyclerViewHolder<NotificationBean> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(NotificationBean data, int position) {
            //0-系统消息，1-预约消息 2-评论消息 3-领取消息 4-关注消息

            setText(R.id.tv_time, data.getCreateTime());
            if(StringUtils.equals("0",messageType)){
                setText(R.id.tv_title, "系统消息");
                setText(R.id.tv_message, data.getTitle());
//                setImageResId(R.id.iv_con,R.mipmap.message_ic_xitong);
            }else if(StringUtils.equals("1",messageType)){
                setText(R.id.tv_title, "预约消息");
                setText(R.id.tv_message, data.getContent());
//                setImageResId(R.id.iv_con,R.mipmap.message_ic_yuyuexiaoxi);
            }else if(StringUtils.equals("2",messageType)){
                setText(R.id.tv_title, "评论消息");
                setText(R.id.tv_message, data.getContent());
//                setImageResId(R.id.iv_con,R.mipmap.message_ic_pinglun);
            }else if(StringUtils.equals("3",messageType)){
                setText(R.id.tv_title, "积分消息");
                setText(R.id.tv_message, data.getContent());
//                setImageResId(R.id.iv_con,R.mipmap.message_ic_jifen);
            }
        }
    }

    public void sortByTime() {
        sortByCreateTime();
        notifyDataSetChanged();
    }

    private void sortByCreateTime() {
        Collections.sort(getDatas(), new Comparator<NotificationBean>() {
            @Override
            //定义一个比较器
            public int compare(NotificationBean o1, NotificationBean o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date dt1 = format.parse(o1.getCreateTime());
                    Date dt2 = format.parse(o2.getCreateTime());
                    if (dt1.getTime() > dt2.getTime()) {
                        return 1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }
}
